﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JCFramework.Extensions
{
    public static class ObjectConvertExtensions
    {
        /// <summary>
        /// object to float type, if not float type ,return float.NaN;
        /// </summary>
        /// <param name="o">object o</param>
        /// <returns>if object is float return float value, if not return float.NaN</returns>
        public static float ToFloat(this object o)
        {
            float result;

            return float.TryParse(o.ToString(), out result) ? result : float.NaN;
        }

        /// <summary>
        /// object to Int32, if Not Int32 Type, return 0
        /// </summary>
        /// <param name="o">object o</param>
        /// <returns>if object is Int32 return Int32 Value, if not return 0</returns>
        public static Int32 ToInt32(this object o)
        {
            Int32 result;

            return Int32.TryParse(o.ToString(), out result) ? result : 0;
        }

        public static long ToLong(this object o)
        {
            long result;

            return long.TryParse(o.ToString(), out result) ? result : 0;
        }
    }
}
